package com.ruoyi.web.controller.system;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.user.TemplateException;
import com.ruoyi.common.utils.file.FastDFSUtil;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain.SysCertificate;
import com.ruoyi.system.domain.SysCertificateTeacher;
import com.ruoyi.system.domain.SysOrganization;
import com.ruoyi.system.domain.Work;
import com.ruoyi.system.service.ISysCertificateService;
import com.ruoyi.system.service.ISysCertificateTeacherService;
import com.ruoyi.system.service.WorkService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 柳
 * @Date 2021/2/24
 * @Description TODO
 */
@RestController
@RequestMapping("/system/certificate")
public class SysCertificateController {

    @Autowired
    private FastDFSUtil fastDFSUtil;

    @Autowired
    private ISysCertificateService sysCertificateService;

    @Autowired
    private WorkService workService;

    @Autowired
    private ISysCertificateTeacherService sysCertificateTeacherService;

    /**
     * 教师页面查询所有证书信息含综合查询
     */
    @ApiOperation("教师页面查询所有证书信息含综合查询")
    @GetMapping("/querySysCertificatesByTeacher")
    public AjaxResult querySysCertificatesByTeacher(SysCertificate sysCertificate){
        PageHelper.startPage(sysCertificate.getCurrentPage(),sysCertificate.getPageSize());
        List<SysCertificate> sysCertificateList = sysCertificateService.querySysCertificatesByTeacher(sysCertificate);
        PageInfo<SysCertificate>  pageInfo=new PageInfo<>(sysCertificateList);
        if(sysCertificateList==null){
            return AjaxResult.error("数据异常,查询失败");
        }

        return AjaxResult.success(pageInfo);
    }

    /**
     * 根据学生id查询该学生的证书信息
     */
    @GetMapping("/querySysCertificateByStuid/{stuid}")
    @ApiOperation("根据学生id查询该学生的证书信息")
    public AjaxResult querySysCertificateByStuid(@PathVariable String stuid) {
        List<SysCertificateTeacher> sysCertificateTeacherList = sysCertificateTeacherService.querySysCertificateTeacherByTid(stuid);
        if(sysCertificateTeacherList.size()==0){
            List<SysCertificate> sysCertificateList = sysCertificateService.querySysCertificateByStuid(stuid);
            return AjaxResult.success(sysCertificateList);
        }
        return  AjaxResult.success(sysCertificateTeacherList);
    }

    /**
     *
     * 修改证书信息
     */
    @PutMapping("/updateSysCertificatesById")
    @ApiOperation("修改证书信息")
    public AjaxResult updateSysCertificatesById(SysCertificate sysCertificate) {
        if(sysCertificate==null){
            return AjaxResult.error("数据异常,操作失败");
        }
        if(StringUtils.isBlank(sysCertificate.getTemplate())){
            return AjaxResult.error("数据异常,操作失败");
        }
        //根据作品id查询作品名称
        Work work = workService.queryWorkByWorkid(sysCertificate);
        //设置证书信息中的作品标题
        sysCertificate.setTitle(work.getTitle());
//        //查询是否有重复证书信息
//        Integer count = sysCertificateService.queryCertificateIsNull(sysCertificate);
//        if(count>0){
//            return AjaxResult.error("请勿重复颁发！！！");
//        }
        //删除原先的pdf图片
        FileUtils.deleteFile(System.getProperty("user.dir")+"\\ruoyi-admin\\src\\main\\resources\\zhengshu\\pdfimage.png");
        //sysCertificateService.querySysCertificatesById(certificateid);
        sysCertificateService.updateSysCertificatesById(sysCertificate);
        return AjaxResult.success();

    }



    /**
     * 根据id查询证书信息
     */
    @GetMapping("/querySysCertificatesById/{certificateid}")
    @ApiOperation("根据id查询证书信息")
    public AjaxResult querySysCertificatesById(@PathVariable Integer certificateid) {
        //sysCertificateService.querySysCertificatesById(certificateid);
        return AjaxResult.success(sysCertificateService.querySysCertificatesById(certificateid));

    }


    /**
     * 删除证书信息
     */
    @DeleteMapping("/deleteSysCertificates/{certificateid}")
    @ApiOperation("删除证书信息")
    public AjaxResult deleteSysCertificates(@PathVariable Integer certificateid) {
        sysCertificateService.deleteSysCertificates(certificateid);
        return AjaxResult.success();

    }


    /**
     * 添加证书信息(颁发证书)
     */
    @PostMapping("/saveSysCertificates")
    @ApiOperation("添加证书信息(颁发证书)")
    public AjaxResult saveSysCertificates(SysCertificate sysCertificate){
        if(sysCertificate==null){
            return AjaxResult.error("数据异常,操作失败");
        }
        if(StringUtils.isBlank(sysCertificate.getTemplate())){
            return AjaxResult.error("数据异常,操作失败");
        }
        //根据作品id查询作品名称
        Work work = workService.queryWorkByWorkid(sysCertificate);
        //设置证书信息中的作品标题
        sysCertificate.setTitle(work.getTitle());
        //查询是否有重复证书信息
        Integer count = sysCertificateService.queryCertificateIsNull(sysCertificate);
        if(count>0){
            return AjaxResult.error("请勿重复颁发！！！");
        }
       //删除原先的pdf图片
        FileUtils.deleteFile(System.getProperty("user.dir")+"\\ruoyi-admin\\src\\main\\resources\\zhengshu\\pdfimage.png");

        try {
            sysCertificateService.saveSysCertificates(sysCertificate);
        } catch (TemplateException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getCode(), e.getMessage());
        }
        return AjaxResult.success();
    }



    /**
     * 查询所有证书信息
     */
    @ApiOperation("查询所有证书信息")
    @GetMapping("/querySysCertificates")
    public AjaxResult querySysCertificates(SysCertificate sysCertificate){
        if(sysCertificate.getCurrentPage()==null || sysCertificate.getPageSize()==null){
            return AjaxResult.error();
        }
        PageHelper.startPage(sysCertificate.getCurrentPage(),sysCertificate.getPageSize());
        List<SysCertificate> sysCertificateList = sysCertificateService.querySysCertificates(sysCertificate);
        PageInfo<SysCertificate>  pageInfo=new PageInfo<>(sysCertificateList);
//        if(sysCertificateList==null){
//            return AjaxResult.error("数据异常,查询失败");
//        }
        return AjaxResult.success(pageInfo);
    }


}
